package main

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"os"
)

const (
	readfilename="./http/原始希腊神华名.txt"
	outfilename="./http/可用神话名.txt"

)
var (
	F *os.File
	List []string
	CanList []string
)

func main() {
	//Check("Hades")
	ReadFile()
	for _, n := range List {
		fmt.Printf("%v\n",n)
		if Check(n){
			CanList=append(CanList,n)
		}
	}
	WriteFile()

}
func ReadFile()  {
	file, err := os.Open(readfilename)
	if err != nil {
		fmt.Print(err)
	}
	defer file.Close()
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		List = append(List,scanner.Text())
	}
}
func WriteFile()  {
	F, err := os.Create(outfilename) //创建文件
	check(err)
	defer F.Close()
	writer := bufio.NewWriter(F)
	for _, v := range CanList {
		writer.WriteString(v)
		writer.WriteString("\n")
		writer.Flush()
	}
}
func Check(val string) bool {
	resp, err := http.PostForm("https://gitee.com/check", url.Values{"do":{"path"},"val":{val}} )
	if err != nil {
		fmt.Printf("错误: %v \n",err)
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		// handle error
	}
	if string(body)=="This url is already taken"||string(body)=="地址已存在" {
		return false
	}
	return true
}
func check(e error) {
	if e != nil {
		fmt.Print(e)
	}
}

